﻿using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.Xml.Schema;
using UnityEngine;
using Color = System.Drawing.Color;

public class GameManager : SingletonMono<GameManager>
{
    public CardLibrarySO Level1EnemyCardLibrary;
    public CardLibrarySO Level2EnemyCardLibrary;
    public CardLibrarySO Level3EnemyCardLibrary;
    public CardLibrarySO Level4EnemyCardLibrary;
    public CardLibrarySO Level5EnemyCardLibrary;
    public CardLibrarySO Level6EnemyCardLibrary;


    public CardLibrarySO Level1AddCardLibrary;
    public CardLibrarySO Level2AddCardLibrary;
    public CardLibrarySO Level3AddCardLibrary;
    public CardLibrarySO Level4AddCardLibrary;
    public CardLibrarySO Level5AddCardLibrary;

    public CardLibrarySO gameCardLibrary;


    public Sprite Level1EnemyHeadSprite;
    public Sprite Level2EnemyHeadSprite;
    public Sprite Level3EnemyHeadSprite;
    public Sprite Level4EnemyHeadSprite;
    public Sprite Level5EnemyHeadSprite;
    public Sprite Level6EnemyHeadSprite;
    
    public SpriteRenderer player2Head;
    public SpriteRenderer player1Head;

    private void OnEnable()
    {
        EventCenter.GetInstance().AddEventListener("Win",gameOver);
        EventCenter.GetInstance().AddEventListener("Win",gameWin);
        EventCenter.GetInstance().AddEventListener("Defeat",gameOver);
        EventCenter.GetInstance().AddEventListener("BeforePlayer1TurnBegin", BeforePlayer1TurnBegin);
        EventCenter.GetInstance().AddEventListener("BeforePlayer2TurnBegin", BeforePlayer2TurnBegin);
    }

    public Color32 Color1 = new Color32(255, 255, 255,255);
    public Color32 Color2 = new Color32(255, 255, 255,125);
    private void BeforePlayer1TurnBegin()
    {
        print(1);
        player1Head.color = Color1;
        player2Head.color = Color2;
    }
    
    private void BeforePlayer2TurnBegin()
    {
        player1Head.color = Color2;
        player2Head.color = Color1;
    }


    private void OnDisable()
    {
        EventCenter.GetInstance().RemoveEventListener("Win",gameOver);
        EventCenter.GetInstance().RemoveEventListener("Win", gameWin);
        EventCenter.GetInstance().RemoveEventListener("Defeat",gameOver);
        EventCenter.GetInstance().RemoveEventListener("BeforePlayer1TurnBegin", BeforePlayer1TurnBegin);
        EventCenter.GetInstance().RemoveEventListener("BeforePlayer2TurnBegin", BeforePlayer2TurnBegin);
    }

    

    public void StartGame(int Level)
    {
        PlayerResourcesManager.GetInstance().GameStart();
        PlayerHealthManager.GetInstance().GameStart();
        switch (Level)
        {
            case 1:
                CardDeck.GetInstance().InitDeck(Level1EnemyCardLibrary);
                player2Head.sprite = Level1EnemyHeadSprite;
                break;
            case 2:
                CardDeck.GetInstance().InitDeck(Level2EnemyCardLibrary);
                player2Head.sprite = Level2EnemyHeadSprite;
                break;
            case 3:
                CardDeck.GetInstance().InitDeck(Level3EnemyCardLibrary);
                player2Head.sprite = Level3EnemyHeadSprite;
                break;
            case 4:
                CardDeck.GetInstance().InitDeck(Level4EnemyCardLibrary);
                player2Head.sprite = Level4EnemyHeadSprite;
                break;
            case 5:
                CardDeck.GetInstance().InitDeck(Level5EnemyCardLibrary);
                player2Head.sprite = Level5EnemyHeadSprite;
                break;
            case 6:
                CardDeck.GetInstance().InitDeck(Level6EnemyCardLibrary);
                player2Head.sprite = Level6EnemyHeadSprite;
                break;
        }
        
        ClickManager.GetInstance().StartGame();
        TurnBaseManager.GetInstance().StartBattle();
    }

    public void gameOver()
    {
        ClickManager.GetInstance().gameStarted = false;
        PlayerResourcesManager.GetInstance().GameOver();
        PlayerHealthManager.GetInstance().GameOver();
        TurnBaseManager.GetInstance().gameOver();
        ChessBoardMgr.GetInstance().GameOver();
        ChessManager.GetInstance().GameOver();
        CardDeck.GetInstance().GameOver();
    }

    [ContextMenu("test")]

    private void gameWin()
    {
        var Level = PatientCount.Instance.patientIndex + 1;
        print(Level);

        switch (Level)
        {
            case 1:
                AddItem(Level1AddCardLibrary.cardLibraryList);
                break;
            case 2:
                AddItem(Level2AddCardLibrary.cardLibraryList);
                break;
            case 3:
                AddItem(Level3AddCardLibrary.cardLibraryList);
                break;
            case 4:
                AddItem(Level4AddCardLibrary.cardLibraryList);
                break;
            case 5:
                AddItem(Level5AddCardLibrary.cardLibraryList);
                break;

        }
    }

    private void AddItem(List<BuildingChessDateSO> list)
    {
        foreach (var date in list)
        {
            if(gameCardLibrary.cardLibraryList.Contains(date) == false)
                gameCardLibrary.cardLibraryList.Add(date);
        }
            
    }
}
